package com.leyou.search.listener;

import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * *
 * Created by IntelliJ IDEA.
 * Author: caoLei
 * Date: 2018/11/2
 * Time: 10:35
 * *
 */
@Component
public class GoodsListener {

    @Autowired
    private SearchService searchService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "leyou.item.goods.save", durable = "true"),
                    exchange = @Exchange(value = "leyou.item.exchange", type = ExchangeTypes.TOPIC, ignoreDeclarationExceptions = "true"),
                    key = {"item.insert"}
            )
    )
    public void save(Long spuId) throws IOException {
        if (spuId == null) {
            return;
        }
        this.searchService.save(spuId);

    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "leyou.item.goods.update", durable = "true"),
                    exchange = @Exchange(value = "leyou.item.exchange", type = ExchangeTypes.TOPIC, ignoreDeclarationExceptions = "true"),
                    key = {"item.update"}
            )
    )
    public void update(Long spuId) throws IOException {
        if (spuId == null) {
            return;
        }
        this.searchService.save(spuId);
    }
}
